const { src, dest,parallel,task } = require('gulp');
const terser = require('gulp-terser');
const concat = require('gulp-concat');
const path = require('path');
const webpack = require('webpack-stream');

const base_path = '../webapp'
const app_files = [
    `${base_path}/js/PreConfig.js`,
    `${base_path}/js/mxClient.min.js`,
    `${base_path}/js/diagramly/Init.js`,
    `${base_path}/js/mxgraph/Init.js`,

    `${base_path}/js/cryptojs/aes.min.js`,
    `${base_path}/js/spin/spin.min.js`,
    `${base_path}/js/deflate/pako.min.js`,
    `${base_path}/js/deflate/base64.js`,
    `${base_path}/js/jscolor/jscolor.js`,
    `${base_path}/js/sanitizer/sanitizer.min.js`,

    `${base_path}/js/mxgraph/Editor.js`,
    `${base_path}/js/mxgraph/EditorUi.js`,
    `${base_path}/js/mxgraph/Sidebar.js`,
    `${base_path}/js/mxgraph/Graph.js`,
    `${base_path}/js/mxgraph/Format.js`,
    `${base_path}/js/mxgraph/Shapes.js`,
    `${base_path}/js/mxgraph/Actions.js`,
    `${base_path}/js/mxgraph/Menus.js`,
    `${base_path}/js/mxgraph/Toolbar.js`,
    `${base_path}/js/mxgraph/Dialogs.js`,

    `${base_path}/js/diagramly/util/mxJsCanvas.js`,
    `${base_path}/js/diagramly/util/mxAsyncCanvas.js`,
    `${base_path}/js/diagramly/DrawioFile.js`,
    `${base_path}/js/diagramly/LocalFile.js`,
    `${base_path}/js/diagramly/LocalLibrary.js`,
    `${base_path}/js/diagramly/StorageFile.js`,
    `${base_path}/js/diagramly/StorageLibrary.js`,
    `${base_path}/js/diagramly/RemoteFile.js`,
    `${base_path}/js/diagramly/RemoteLibrary.js`,
    `${base_path}/js/diagramly/Dialogs.js`,
    `${base_path}/js/diagramly/Editor.js`,
    `${base_path}/js/diagramly/EditorUi.js`,
    `${base_path}/js/diagramly/DiffSync.js`,
    `${base_path}/js/diagramly/Settings.js`,
    `${base_path}/js/diagramly/DrawioFileSync.js`,
    `${base_path}/js/diagramly/DrawioComment.js`,
    `${base_path}/js/diagramly/DriveComment.js`,
    `${base_path}/js/diagramly/DrawioClient.js`,
    `${base_path}/js/diagramly/DrawioUser.js`,
    `${base_path}/js/diagramly/UrlLibrary.js`,

    `${base_path}/js/diagramly/App.js`,
    `${base_path}/js/diagramly/Menus.js`,
    `${base_path}/js/diagramly/Pages.js`,
    `${base_path}/js/diagramly/Trees.js`,
    `${base_path}/js/diagramly/Minimal.js`,
    `${base_path}/js/diagramly/DistanceGuides.js`,
    `${base_path}/js/diagramly/mxRuler.js`,
    `${base_path}/js/diagramly/mxFreehand.js`,
    `${base_path}/js/diagramly/DevTools.js`,
    `${base_path}/js/diagramly/cmap/importer.js`,
    `${base_path}/js/diagramly/NextcloudClient.js`,
    `${base_path}/js/lib/dataTables.altEditor.free.js`,

    `${base_path}/js/PostConfig.js`,
]

const vendor_files = [
    `${base_path}/js/jquery-3.4.1.min.js`,
    `${base_path}/js/jquery.dataTables.min.js`,
    `${base_path}/js/dataTables.buttons.min.js`,
    `${base_path}/js/dataTables.select.min.js`,
    `${base_path}/js/dataTables.responsive.min.js`,
    `${base_path}/js/popper.min.js`,
    `${base_path}/js/bootstrap.min.js`,
    `${base_path}/js/crypto-js.min.js`,
    `${base_path}/js/bootstrap-table.js`
]

const vendor_css_files = [
    `${base_path}/styles/bootstrap.css`,
    `${base_path}/styles/font-awesome.min.css`,
    `${base_path}/styles/jquery.dataTables.min.css`,
    `${base_path}/styles/buttons.dataTables.min.css`,
    `${base_path}/styles/responsive.dataTables.min.css`,
    `${base_path}/styles/select.dataTables.min.css`,
    `${base_path}/styles/bootstrap-table.min.css`
];

const buildCss = (srcFiles,target)=>{
    return src(srcFiles)
        .pipe(concat(path.basename(target)))
        .pipe(dest(path.dirname(target)))
}

const buildJs = (srcFiles,target)=>{
    return src(srcFiles)
        .pipe(concat(path.basename(target)))
        .pipe(terser())
        .pipe(dest(path.dirname(target)))
}

function buildVendor(){
    return buildJs(vendor_files,`${base_path}/js/vendor.min.js`)
}
function buildInit(){
    return buildJs([`${base_path}/js/init.js`],`${base_path}/js/init.min.js`)
}
function buildApp(){
    return buildJs(app_files,`${base_path}/js/app.min.js`)
}
function buildPlugin(){
    return buildJs([`${base_path}/plugins/animation.js`],`${base_path}/js/plugin.min.js`)
}
function buildVendorCss(){
    return buildCss(vendor_css_files,`${base_path}/styles/vendor.min.css`)
}
function buildMain(){
    return src(`${base_path}/js/main.js`)
        .pipe(webpack({
            output: {
                filename: 'main.min.js'
            }
        }))
        .pipe(dest(`${base_path}/js`));
}

const rebuildVendor = process.env['rebuildVendor']||false;
let tasks = parallel(buildInit,buildApp,buildMain,buildPlugin)
if(rebuildVendor){
    tasks = parallel(buildVendor,buildVendorCss,tasks)
}
exports.default = tasks
